/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientFrame;

import ejb.sessions.TDSessionRemote;
import views.ComboBoxCellRender;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import models.TravelWayDTO;
import models.cittaDTO;
import models.TripDTO;

/**
 *
 * @author simone
 */
public class TravelDialog extends javax.swing.JDialog {

    ArrayList<cittaDTO> cities;
    ArrayList<TravelWayDTO> tWays;

    /**
     * Creates new form TravelDialog
     * @param parent
     * @param modal
     */
    public TravelDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        fillCityBox();
        fillTypeTravelBox();
        setCellRender();
    }
    
    private void setCellRender()
    {
        ComboBoxCellRender cellrender = new ComboBoxCellRender();
        typeTripBox.setRenderer(cellrender);
        cittaArrBox.setRenderer(cellrender);
        cittaDepBox.setRenderer(cellrender);  
    }

    private void fillCityBox()
    {
        TDSessionRemote session = lookupTDSessionRemote();
        this.cities = session.getAllCity();
        for (cittaDTO c : cities) 
        {
            cittaDepBox.addItem(c);
            cittaArrBox.addItem(c);
        }
    }
    
    private void fillTypeTravelBox()
    {
        TDSessionRemote session = lookupTDSessionRemote();
        this.tWays = session.getAllTravelType();
        for (TravelWayDTO tw : tWays) {
            typeTripBox.addItem(tw);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        costField = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cittaDepBox = new javax.swing.JComboBox();
        cittaArrBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        typeTripBox = new javax.swing.JComboBox();
        createTravelBtn = new javax.swing.JButton();
        errLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(315, 260));

        jLabel1.setText("Costo:");

        costField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        jLabel2.setText("Città Partenza:");

        jLabel3.setText("Città Arrivo:");

        jLabel4.setText("Tipo Viaggio:");

        createTravelBtn.setText("Create");
        createTravelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createTravelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cittaArrBox, 0, 198, Short.MAX_VALUE)
                            .addComponent(typeTripBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cittaDepBox, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(21, 21, 21)
                            .addComponent(costField))
                        .addComponent(errLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(createTravelBtn)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(typeTripBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cittaDepBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cittaArrBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(costField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(errLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(createTravelBtn))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void createTravelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createTravelBtnActionPerformed
        // TODO add your handling code here:
        Object costObj = costField.getValue();
        cittaDTO cityArr = (cittaDTO) cittaArrBox.getSelectedItem();
        cittaDTO cityDep = (cittaDTO) cittaDepBox.getSelectedItem();
        TravelWayDTO typeTrip  = (TravelWayDTO) typeTripBox.getSelectedItem();
        
        if(costObj==null )
        {
            errLabel.setText("missing field");
            return;
        }
        
        TripDTO trip = new TripDTO();
        trip.setCost(costObj);
        trip.setCittaArr(cityArr);
        trip.setCittaDep(cityDep);
        trip.settWay(typeTrip);
        TDSessionRemote session = lookupTDSessionRemote();
        boolean created = session.createTrip(trip);

    }//GEN-LAST:event_createTravelBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cittaArrBox;
    private javax.swing.JComboBox cittaDepBox;
    private javax.swing.JFormattedTextField costField;
    private javax.swing.JButton createTravelBtn;
    private javax.swing.JLabel errLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JComboBox typeTripBox;
    // End of variables declaration//GEN-END:variables

    private TDSessionRemote lookupTDSessionRemote() {
        try {
            Context c = new InitialContext();
            return (TDSessionRemote) c.lookup("java:comp/env/TDSession");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}

